//
//  readViewController.m
//  read
//
//  Created by 宋 on 15/12/15.
//  Copyright © 2015年 宋. All rights reserved.
//

#import "readViewController.h"
#import "txtViewController.h"
//分享@protocol UIActivityItemSource <NSObject>

@interface myItem : NSObject <UIActivityItemSource>
@property (strong,nonatomic)NSString * text;
@end

@implementation myItem
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    return self.text;
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
    return  self.text;
}

@end

@interface readViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
{
    UIPageViewController * pageview;
    NSUInteger page;
    NSUInteger allpage;
    NSInteger lengths;
    CGFloat heights;
    UIBarButtonItem * button;
    UIToolbar *tbar;
}
@end

@implementation readViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(onClick:)];
    
    pageview = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionSpineLocationKey : @(UIPageViewControllerSpineLocationNone)}];
    pageview.dataSource = self;
    pageview.delegate = self;
    [self addChildViewController:pageview];
    pageview.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 );
 
    
    
//    pageview.view.transform = CGAffineTransformMakeRotation(M_PI/6);
   
//    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/6);
//    transform = CGAffineTransformScale(transform, 0.8, 0.8);
//    pageview.view.transform = transform;

    [self.view addSubview:pageview.view];

    //分享
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(onlin:)];
    
    
     tbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44 )];
    [self.view addSubview:tbar];

    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[[UILabel new].font fontWithSize:24.0]} context:nil];
    heights = ceil(rect.size.height);
    allpage = ceil(heights / (self.view.frame.size.height - 64 - 44));
    lengths = _text.length / allpage;
    page = 0;
    
   
    button = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"%lu/%lu",page + 1,(unsigned long)allpage] style:UIBarButtonItemStylePlain target:nil action:nil];
    tbar.items = @[button];
    
    txtViewController * con = [txtViewController new];
    con.text = [self.text  substringWithRange:NSMakeRange(page * lengths, lengths)];
    [pageview setViewControllers:@[con] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

}

- (void)onlin:(id)sender
{
    myItem * myitem = [myItem new];
    myitem.text = @"!!!";
    UIActivityViewController * con =[[UIActivityViewController alloc]initWithActivityItems:@[myitem] applicationActivities:nil];
    [self presentViewController:con animated:YES completion:nil];
}

- (void)up
{
    button.title = [NSString  stringWithFormat:@"%lu/%lu",page,(unsigned long)allpage];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if (page <= 0) {
        return nil;
    }
    page--;
    [self up];
    return [self donw];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if (page >= allpage - 1) {
        return nil;
    }
    page++;
    [self up];
    return [self donw];
}

- (UIViewController *)donw
{
    txtViewController * con = [txtViewController new];
    con.text = [self.text substringWithRange:NSMakeRange(page * lengths, lengths)];
    con.index = &(page);
    return con;
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
{
    if (!completed) {
        txtViewController * con = (txtViewController *)previousViewControllers[0];
        page = *(con.index);
        [self up];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
